﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace SdmxMl.GenericData
{
    /// <summary>
    /// Helper for tags containing generic Values tag
    /// </summary>
    public class ValueListTag
    {
        #region Properties

        private string _tag;

        private ValueTypeList _valueList;
        public ValueTypeList ValueList
        {
            get { return _valueList; }
        }

        #endregion //Properties

        #region Construction

        private void Init(string t)
        {
            _tag = t;
            _valueList = new ValueTypeList();
        }

        public ValueListTag(string t)
        {
            Init(t);
        }

        public ValueListTag(string t, XmlNode nodeArtefact, XmlNamespaceManager nsmgr)
        {
            XmlNodeList nodeList = null;
            Init(t);

            // list values
            if (nodeArtefact.SelectSingleNode("./generic:" + _tag, nsmgr) != null)
            {
                nodeList = nodeArtefact.SelectNodes("./generic:" + _tag + "/generic:Value", nsmgr);
                foreach (XmlNode nm in nodeList)
                {
                    ValueType v = new ValueType(nm, nsmgr);
                    ValueList.Add(v);
                }
            }
        }

        #endregion //Construction

        #region XML serialize
        public void WriteXml(XmlTextWriter xw)
        {
            string prefix = "generic";

            // Reported Attributes 
            if (ValueList.Count > 0)
            {
                xw.WriteStartElement(prefix, _tag, null);
                foreach (ValueType v in ValueList)
                    v.WriteXml(xw, prefix, "Value");
                xw.WriteEndElement();
            }
        }

        #endregion //XML serialize
    }

}
